import { Popconfirm, List } from "antd";

import useAllMembers from "../../hooks/member/useAllMembers";
import useAddStaff from "../../hooks/staff/useAddStaff";

const AddStaff = ({ staffs, reloadData }) => {
    const { loading, members, loadMembers } = useAllMembers();
    const { loading: submitLoading, addStaff } = useAddStaff();

    let handleAdd = async (item) => {
        let variables = {
            ...item,
            osFrom: "来自成员",
            osStat: true,
            osType: "正式",
        };
        addStaff(variables, (res) => {
            if (res) {
                loadMembers();
                reloadData({});
            }
        });
    };

    return (
        <div style={{ padding: "10px 0" }}>
            <List
                loading={loading}
                header={"成员列表"}
                bordered
                size="small"
                dataSource={members}
                renderItem={(item) => {
                    let actions = [];
                    if (staffs.findIndex((_item) => _item.omUuid === item.omUuid) === -1) {
                        actions.push(
                            <Popconfirm title="确定要添加当前人员吗?" placement="topRight" okButtonProps={{ loading: submitLoading }} onConfirm={() => handleAdd(item)}>
                                <a>添加</a>
                            </Popconfirm>
                        );
                    } else {
                        // actions.push("已添加");
                        return null;
                    }
                    return <List.Item actions={actions}>{item.omName}</List.Item>;
                }}
            />
        </div>
    );
};

export default AddStaff;
